#include <iostream>
#include <memory>
#include "HttpServer.hpp"

static void Usage(std::string proc)
{
    std::cout << "Usage: \n\t" << proc << " port" << std::endl;
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }

    std::shared_ptr<HttpServer> http_server(new HttpServer(atoi(argv[1])));
    http_server->InitHttpServer();
    http_server->Loop();
    
    int fd = open("log.txt", O_RDONLY);
    std::string out;
    while(true)
    {
        char buffer[1024];
        ssize_t s = read(fd, buffer, sizeof(buffer) - 1);
        if(s == 0)
        {
            close(fd);
        }
        else if(s < 0)
        {
            if(errno == EAGAIN || errno == EWOULDBLOCK)
              continue;
            else break;
        }
        else
        {
            buffer[s] = '\0';
            out += buffer; 
        }
    }

    std::cout << out << std::endl;
    return 0;
}
